package com.kuang.annotation;

import com.sun.org.apache.xpath.internal.SourceTree;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Test08 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
        Class c1 = Class.forName("com.kuang.annotation.Person");

//        Person p1 = new Person();
//        c1 = p1.getClass();
//        System.out.println(p1.getClass().getName());
//        System.out.println(p1.getClass().getSimpleName());

        //获取类的名字
        System.out.println(c1.getName());//包名+类名
        System.out.println(c1.getSimpleName());//类名
        System.out.println("**************************");
        //获得类的属性
        Field[] fields = c1.getFields();//只能找到public属性
        fields = c1.getDeclaredFields();//找到全部的属性
        for (Field field : fields) {
            System.out.println(field);
        }
//        Field name = c1.getField("name");//找不到
        //Field获取指定属性的值
        Field name = c1.getDeclaredField("name");
        System.out.println(name);
        System.out.println("********************");
        //获得所有的方法
        //获得本类极其父类的全部public方法
        Method[] methods = c1.getMethods();
        for (Method method : methods) {
            System.out.println("正常的方法:"+method);
        }
        System.out.println("********************");
        //获得本类的所有方法(不包括父类的 public private等)
         methods= c1.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println("getDeclaredMethods:"+method);
        }
        System.out.println("********************");
        //获得指定方法public
        Method getName = c1.getMethod("getName",null);
        System.out.println(getName);
        System.out.println("********************");
        Method setName = c1.getMethod("setName", String.class);
        System.out.println(setName);
        //获得指定方法,private
        System.out.println("********************");
        Method test = c1.getDeclaredMethod("test");
        System.out.println(test);
        System.out.println("==================================");

        //获得构造器(public)
        Constructor[] constructors = c1.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println("构造器有(public)"+constructor);
        }
        System.out.println("********************");
                //获得构造器()
                Constructor[] declaredConstructors = c1.getDeclaredConstructors();
                for (Constructor declaredConstructor : declaredConstructors) {
                    System.out.println("全部构造器"+declaredConstructor);
        }
        System.out.println("********************");
        //获得指定构造器()
        Constructor declaredConstructor = c1.getDeclaredConstructor(String.class, int.class, int.class);
        System.out.println(declaredConstructor);
    }
}
